import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

// ignore: must_be_immutable
class CustomSlider extends StatefulWidget {
  bool isDisable;
  double progressValue;
  ValueChanged<double> onProgressValueChanged;
  ValueChanged<Color> onColorChanged;
  CustomSlider({Key key, this.isDisable=false,this.progressValue,this.onProgressValueChanged,this.onColorChanged}) : super(key: key);
  @override
  _CustomSliderState createState() => _CustomSliderState();
}

class _CustomSliderState extends State<CustomSlider> {
  double _hue;
  double lightness = 0.5;
  Color get _color {
    return HSLColor.fromAHSL(
      1,
      _hue,
      1,
      lightness,
    ).toColor();
  }
  @override
  Widget build(BuildContext context) {
    return Container(
      width:MediaQuery.of(context).size.width,
      alignment: Alignment.center,
      child: Stack(
        alignment: Alignment.center,
        children: [
          widget.isDisable == false?Container(
            height: 8,
            width: 320,
            decoration: BoxDecoration(
              color: Colors.white,
              borderRadius: BorderRadius.circular(8),
              gradient: LinearGradient(
                begin: Alignment.centerLeft,
                end: Alignment.centerRight,
                stops: [
                  0,
                  5 / 360,
                  (5 + 350 / 6) / 360,
                  (5 + (2 * 350) / 6) / 360,
                  (5 + (3 * 350) / 6) / 360,
                  (5 + (4 * 350) / 6) / 360,
                  (5 + (5 * 350) / 6) / 360,
                  (5 + (6 * 350) / 6) / 360,
                  1.0
                ],
                colors: [
                  Color.fromARGB(255, 255, 0, 0),
                  Color.fromARGB(255, 255, 0, 0),
                  Color.fromARGB(255, 255, 255, 0),
                  Color.fromARGB(255, 0, 255, 0),
                  Color.fromARGB(255, 0, 255, 255),
                  Color.fromARGB(255, 0, 0, 255),
                  Color.fromARGB(255, 255, 0, 255),
                  Color.fromARGB(255, 255, 0, 0),
                  Color.fromARGB(255, 255, 0, 0),
                ],
              ),
            ),
          ):SizedBox(),
          SliderTheme(
            data: SliderThemeData(
              trackHeight: 5,
              activeTrackColor: Colors.transparent,
              thumbColor:Colors.white,
              inactiveTrackColor: Colors.transparent,
              disabledActiveTrackColor: Colors.grey,
              disabledInactiveTrackColor: Colors.black38,
              overlayShape: RoundSliderOverlayShape(),
              showValueIndicator: ShowValueIndicator.onlyForDiscrete,
            ),
            child: new Slider(
              value: widget.progressValue,
              label: '${widget.progressValue}',
              min: 0.0,
              max: 360.0,
              divisions: 1000,
              onChanged: widget.isDisable == false?(double) {
                setState(() {
                  widget.progressValue = double.roundToDouble();
                  _hue = widget.progressValue;
                });
                print('_color${_color}');
                print('_hue${_hue}');
                widget.onProgressValueChanged(widget.progressValue);
                widget.onColorChanged(_color);
              }:null,
            ),
          ),
        ],
      ),
    );
  }
}
